﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using System.Data;
using System.Text;

namespace TravelExpertsASP
{
    public partial class clientProfile : System.Web.UI.Page
    {
        DbAccess myAccess = new DbAccess();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["isSessionSet"] != null)
            {
                if (Session["isSessionSet"] == "Flag")
                {
                    //Valid User
                    
                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
                //Session expired
            }

            if (!IsPostBack)
            {
                refreshProfile();
            }
           

            // ////get the past order list to tale//////////////////
            ////////////Added by Qingshenrg Zhou(Jason)////////////////////////
            string eAddr = Session["CustEmail"].ToString();
            DataTable newView = new DataTable();
            newView = myAccess.selectCustFromEmail(eAddr);
            myTable.DataSource = newView;
            myTable.DataBind();
            decimal result = 0;

            ////////////////get the total price of one customer///////////////
            ////////////Added by Qingshenrg Zhou(Jason)////////////////////////
            for (int i = 0; i < newView.Rows.Count; i++)
            {
                result += Convert.ToDecimal(newView.Rows[i][3]);
            }

            lblPrice.Text = "Total Purchases: "  + result.ToString("C");
             


        }

        // /////////get the customer update information////////////////////
        ////////////Added by Qingshenrg Zhou(Jason)////////////////////////
        private void refreshProfile()
        {
            ////////////Added by Qingshenrg Zhou(Jason)////////////////////////
            string eAddr = Session["CustEmail"].ToString();
            //string eAddr = Request.QueryString["CustEmail"];
            // ////get the customer information to the list/////////
            Customer newCustomer = new Customer();
            newCustomer = myAccess.custInfo(eAddr);
            txtFname.Text = newCustomer.CustFirstName;
            txtLname.Text = newCustomer.CustLastName;
            txtAddr.Text = newCustomer.CustAddress;
            txtCity.Text = newCustomer.CustCity;
            txtProv.Text = newCustomer.CustProv;
            txtCountry.Text = newCustomer.CustCountry;
            lblUserName.Text = "Welcome, " + newCustomer.CustFirstName;

        }
        private Customer CustDetails()
        {
           
            Customer myCust = new Customer();
           
            myCust.CustFirstName = txtFname.Text;
            myCust.CustLastName = txtLname.Text;
            myCust.CustAddress = txtAddr.Text;
            myCust.CustCity = txtCity.Text;
            myCust.CustProv = txtProv.Text;
            myCust.CustCountry = txtCountry.Text;
            myCust.CustEmail = Session["CustEmail"].ToString();

            return myCust;
        }
        // /////////Update the customer update information////////////////////
        protected void Update_Click(object sender, EventArgs e)
        {
            myAccess.UpdateCustomers(CustDetails());
            refreshProfile();
        }


    }
}